package com.tiantianyuegou.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.SelfUtils;
import com.tiantianyuegou.util.Tools;
import com.tiantianyuegou.R;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
//import com.trouble.framework.utils.MD5;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 安全中心修改密码
 */
public class MyChangePassWordActivity extends BaseActivity implements View.OnClickListener, TextWatcher {

    private ImageView img_title;
    private TextView text_title;
    private LoadingDialog loadingDialog;
    private EditText edit_old_password, edit_new_password, edit_new2_password;
    private Button btn_change;

    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_change_pass_word);
        init();
        Tools.changeSystemBar(this);
    }

    @Override
    public void findViewByIds() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void loadPassWord() {
     loadingDialog = new LoadingDialog(this);
        loadingDialog.setMsg("正在修改...");
        loadingDialog.show();
        if (id.equals("login")) {
            RequestParams params = new RequestParams();
            params.addBodyParameter("tmId", SPHelper.getTmId());
            params.addBodyParameter("oldPassword", SPHelper.getPassword());
            params.addBodyParameter("newPassword", edit_new2_password.getText().toString());
            Log.e("Login  ", new SelfUtils().getMD5(SPHelper.getPassword()));
            loadData(HttpRequest.HttpMethod.POST, ApiDefine.RESET_PWD, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    loadingDialog.dismiss();
                    String response = responseInfo.result;
                    Log.e("Login", response);
                    try {
                        JSONObject object = new JSONObject(response);
                        if ("100".equals(object.getString("code"))) {
                            showMsg("重置登录密码成功");
                            SPHelper.setPassword(edit_new2_password.getText().toString());
                            finish();
                        } else {
                            showMsg(object.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFailure(HttpException e, String s) {
                    loadingDialog.dismiss();
                }
            });

        } else if (id.equals("pay")) {
            RequestParams params = new RequestParams();
            params.addBodyParameter("tmId", SPHelper.getTmId());
            params.addBodyParameter("oldPassword", edit_old_password.getText().toString());
            params.addBodyParameter("newPassword", edit_new_password.getText().toString());
//            params.addBodyParameter("password", MD5.md5(SPHelper.getPassword()).toLowerCase());
//            Log.e("TAG", "reset pay pwd:" + MD5.md5(SPHelper.getPassword()).toLowerCase());
            loadData(HttpRequest.HttpMethod.POST, ApiDefine.RESET_PAY_PWD, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    loadingDialog.dismiss();
                    String response = responseInfo.result;
                    Log.e("pay", response);
                    if (TextUtils.isEmpty(responseInfo.result)) {
                        showMsg("重置支付密码失败");
                    } else {
                        try {
                            JSONObject root = new JSONObject(responseInfo.result);
                            String code = root.getString("code");
                            if ("100".equals(code)) {
                                showMsg("重置支付密码成功");
                                finish();
                            } else {
                                String msg = root.getString("message");
                                showMsg(msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            showMsg("解析异常");
                        }
                    }
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    loadingDialog.dismiss();
                    showMsg("当前网络不可用,请检查您的网络设置");
                }
            });
        }


    }

    private void init() {
        text_title = (TextView) findViewById(R.id.text_title);
        img_title = (ImageView) findViewById(R.id.img_title);

        img_title.setOnClickListener(this);

        edit_old_password = (EditText) findViewById(R.id.edit_old_password);
        edit_new_password = (EditText) findViewById(R.id.edit_new_password);
        edit_new2_password = (EditText) findViewById(R.id.edit_new2_password);

        btn_change = (Button) findViewById(R.id.btn_change);


        id = getIntent().getStringExtra("id");

        edit_old_password.addTextChangedListener(this);
        edit_new_password.addTextChangedListener(this);
        edit_new2_password.addTextChangedListener(this);
        if ("login".equals(id)) {
            text_title.setText(R.string.change_login_title);
            edit_old_password.setHint(R.string.change_login_old);
            edit_new_password.setHint(R.string.change_login_new);
            edit_new2_password.setHint(R.string.change_login_new2);
        } else if ("pay".equals(id)) {
            text_title.setText(R.string.change_pay_title);

            edit_old_password.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
            edit_new_password.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
            edit_new2_password.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});

            edit_old_password.setInputType(EditorInfo.TYPE_CLASS_PHONE);
            edit_new_password.setInputType(EditorInfo.TYPE_CLASS_PHONE);
            edit_new2_password.setInputType(EditorInfo.TYPE_CLASS_PHONE);

            edit_old_password.setHint(R.string.change_pay_old);
            edit_new_password.setHint(R.string.change_pay_new);
            edit_new2_password.setHint(R.string.change_pay_new2);

        }


        btn_change.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.img_title:
                finish();
                break;

            case R.id.btn_change:

                String old_password = edit_old_password.getText().toString();
                String new_password = edit_new_password.getText().toString();
                String new2_password = edit_new2_password.getText().toString();

                if ("login".equals(id)) {
                    if (TextUtils.isEmpty(old_password)) {
                        showMsg("请输入原密码");
                    } else if (old_password.length() < 6 || old_password.length() > 20) {
                        showMsg("密码长度应该为6至20位");
                    } else if (SelfUtils.isConSpeCharacters(old_password)) {
                        showMsg("密码中不能包含特殊字符");
                    } else if (!SPHelper.getPassword().equals(old_password)) {
                        showMsg("原密码不正确");
                    } else if (TextUtils.isEmpty(new_password) || TextUtils.isEmpty(new2_password)) {
                        showMsg("新密码不能为空");
                    } else if (new_password.length() < 6 || new_password.length() > 20 || new2_password.length() < 6 || new2_password.length() > 20) {
                        showMsg("密码长度应该为6至20位");
                    } else if (SelfUtils.isConSpeCharacters(new_password) || SelfUtils.isConSpeCharacters(new2_password)) {
                        showMsg("新密码中不能包含特殊字符");
                    } else if (!new_password.equals(new2_password)) {
                        showMsg("两次输入的密码不一致");
                    } else {
                        loadPassWord();
                    }
                } else if ("pay".equals(id)) {
                    if (TextUtils.isEmpty(old_password)) {
                        showMsg("请输入原密码");
                    } else if (old_password.length() > 6 || old_password.length() < 6) {
                        showMsg("原密码长度只能为6位");
                    } else if (new_password.length() > 6 || new_password.length() < 6 || new2_password.length() > 6 || new2_password.length() < 6) {
                        showMsg("新密码长度只能为六位");
                    } else if (!new_password.equals(new2_password)) {
                        showMsg("两次输入的密码不一致");
                    } else {
                        loadPassWord();
                    }
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        String old_password = edit_old_password.getText().toString();
        String new_password = edit_new_password.getText().toString();
        String new2_password = edit_new2_password.getText().toString();
        if (TextUtils.isEmpty(old_password) || TextUtils.isEmpty(new_password) || TextUtils.isEmpty(new2_password)) {
            btn_change.setEnabled(false);
            btn_change.setAlpha(0.5f);
        } else {
            btn_change.setEnabled(true);
            btn_change.setAlpha(1.0f);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }


}
